package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Cateinfo;
import com.beans.Roleinfo;
import com.dao.CateDao;
import com.dao.GoodsDao;

@WebServlet("/CateServlet.do")
public class CateServlet extends HttpServlet {
	private CateDao _cateDao = new CateDao();
	private GoodsDao _goodsDao=new GoodsDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String flag = request.getParameter("flag");
		if ("bigcateAdd".equals(flag)) {
			bigcateAdd(request, response);
		} else if ("smallcateAdd".equals(flag)) {
			smallcateAdd(request, response);
		} else if ("manage".equals(flag)) {
			manage(request, response);
		} else if ("bigcateEdit".equals(flag)) {
			bigcateEdit(request, response);
		} else if ("bigcateDelet".equals(flag)) {
			bigcateDelet(request, response);
		} else if ("smallcateEdit".equals(flag)) {
			smallcateEdit(request, response);
		} else if ("smallcateDelet".equals(flag)) {
			smallcateDelet(request, response);
		} else if ("smallcateUpdate".equals(flag)) {
			smallcateUpdate(request, response);
		} else if ("bigcateUpdate".equals(flag)) {
			bigcateUpdate(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/*
	 * 一级商品分类添加
	 */
	private void bigcateAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cateName = request.getParameter("cateName");
		String des = request.getParameter("des");
		Cateinfo cateinfo = new Cateinfo();
		cateinfo.setCateName(cateName);
		cateinfo.setDes(des);
		cateinfo.setParentId(0);
		int reat = _cateDao.addCate(cateinfo);
		if (reat == 1) {
			request.setAttribute("msg", "分类创建成功");
			request.getRequestDispatcher("/goods/bigcate_add.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "分类创建失败");
			request.getRequestDispatcher("/goods/bigcate_add.jsp").forward(request, response);
		}
	}

	/*
	 * 二级商品分类添加
	 */
	private void smallcateAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String parentCate = request.getParameter("parentCate");
		Integer parentId = Integer.parseInt(parentCate);
		String cateName = request.getParameter("cateName");
		String des = request.getParameter("des");
		Cateinfo cateinfo = new Cateinfo();
		cateinfo.setCateName(cateName);
		cateinfo.setDes(des);
		cateinfo.setParentId(parentId);
		int reat = _cateDao.addCate(cateinfo);
		if (reat == 1) {
			request.setAttribute("msg", "分类创建成功");
			request.getRequestDispatcher("/goods/smallcate_add.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "分类创建失败");
			request.getRequestDispatcher("/goods/smallcate_add.jsp").forward(request, response);
		}
	}

	/*
	 * 商品管理
	 */
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Cateinfo> cateList = _cateDao.getAllCates();
		request.setAttribute("cateList", cateList);
		request.getRequestDispatcher("/goods/cate_manage.jsp").forward(request, response);
	}

	/*
	 * 一级商品分类 修改页
	 */
	private void bigcateEdit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		Cateinfo cate=_cateDao.getCate(idnumber);
		request.setAttribute("cate", cate);
		request.getRequestDispatcher("/goods/bigcate_edit.jsp").forward(request, response);
	}

	/*
	 * 一级商品分类 删除
	 */
	private void bigcateDelet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int idInt=Integer.parseInt(id);
		if(_cateDao.getCateCount(idInt)==0) {
			int resut=_cateDao.delCate(idInt);
			if (resut==1) {
				manage(request, response);
			}
		}else {
			request.setAttribute("msg", "请先将对应二级商品分类删除或修改");
			manage(request, response);
		}
	}

	/*
	 * 一级商品分类 修改
	 */
	private void bigcateUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		String cateName = request.getParameter("cateName");
		String des = request.getParameter("des");
		int idnumber = Integer.parseInt(id);
		Cateinfo cateinfo=new Cateinfo();
		cateinfo.setId(idnumber);
		cateinfo.setDes(des);
		cateinfo.setCateName(cateName);
		cateinfo.setParentId(0);//本身为父级，为0
		int result =_cateDao.updateCate(cateinfo);
		if (result == 1) {
			response.getWriter().print("商品分类更新成功");
		}else {
			response.getWriter().print("商品分类更新失败");
		}
	}

	/*
	 * 二级商品分类 修改页
	 */
	private void smallcateEdit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		Cateinfo cate=_cateDao.getCate(idnumber);
		request.setAttribute("cate", cate);
		request.getRequestDispatcher("/goods/smallcate_edit.jsp").forward(request, response);
	}

	/*
	 * 二级商品分类 删除
	 */
	private void smallcateDelet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		if(_goodsDao.getGoodsCount(id)==0) {
			int idInt=Integer.parseInt(id);
			int resut=_cateDao.delCate(idInt);
			if (resut==1) {
				manage(request, response);
			}
		}else {
			request.setAttribute("msg", "请先将商品管理中对应的分类进行修改或删除");
			manage(request, response);
		}
	}

	/*
	 * 二级商品分类 修改
	 */
	private void smallcateUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		String parentId = request.getParameter("parentId");
		String cateName = request.getParameter("cateName");
		String des = request.getParameter("des");
		int idnumber = Integer.parseInt(id);
		int parentIdnumber = Integer.parseInt(parentId);
		Cateinfo cateinfo=new Cateinfo();
		cateinfo.setId(idnumber);
		cateinfo.setDes(des);
		cateinfo.setCateName(cateName);
		cateinfo.setParentId(parentIdnumber);
		int result =_cateDao.updateCate(cateinfo);
		if (result == 1) {
			response.getWriter().print("商品分类更新成功");
		}else {
			response.getWriter().print("商品分类更新失败");
		}

	}
}
